//
//  AddAddressController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/4.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "AddAddressController.h"
#import "UITextView+ZWPlaceHolder.h"
#import "NetLoadAreaPickerViewController.h"
@interface AddAddressController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *phoneTextField;
@property (weak, nonatomic) IBOutlet UITextField *addressTextField;
@property (weak, nonatomic) IBOutlet UITextView *addressTextView;
@property (weak, nonatomic) IBOutlet UIButton *defaulButton;

@property (nonatomic,strong) NSString *areaID;
@end

@implementation AddAddressController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新增收货地址";
    [self.leftbutton setImage:imgname(@"lefticon") forState:(UIControlStateNormal)];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    self.addressTextView.placeholder = @"请输入详细地址";
    
    [self.defaulButton setImageEdgeInsets:UIEdgeInsetsMake(0.0, -10, 0.0, 0.0)];
    self.areaID = @"";
}
- (IBAction)selectAddressButton:(UIButton *)sender {
    NetLoadAreaPickerViewController * loadAreaVC = [[NetLoadAreaPickerViewController alloc]init];
    loadAreaVC.selectedBlock = ^(NetLoadAreaModel *areaModel) {
        
        NSString *areaName = [NSString stringWithFormat:@"%@%@%@", areaModel.provinceName, areaModel.cityName, areaModel.districtName];
        self.addressTextField.text = areaName;
        self.areaID = areaModel.districtCode;
//        NSLog(@"%@",areaModel.districtCode);
    };
    if ([[[UIDevice currentDevice] systemVersion] floatValue]>=8.0) {
        loadAreaVC.modalPresentationStyle=UIModalPresentationOverCurrentContext;
    }else{
        loadAreaVC.modalPresentationStyle=UIModalPresentationCurrentContext;
    }
    [self presentViewController:loadAreaVC animated:YES completion:nil];
    return;
}
- (IBAction)defaulButtonClick:(UIButton *)sender {
    self.defaulButton.selected =! self.defaulButton.selected;
}
- (IBAction)commitButtonClick:(UIButton *)sender {
    self.nameTextField.text = [self filterSpaces:self.nameTextField.text];
    self.nameTextField.text = [self disable_emoji:self.nameTextField.text];
    
    self.phoneTextField.text = [self filterSpaces:self.phoneTextField.text];
    self.phoneTextField.text = [self disable_emoji:self.phoneTextField.text];
    
    self.addressTextView.text = [self filterSpaces:self.addressTextView.text];
    self.addressTextView.text = [self disable_emoji:self.addressTextView.text];
    
    if ([self.nameTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入姓名");
        return;
    }
    if ([self.phoneTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入联系电话");
        return;
    }
    if ([self.areaID isEqualToString:@""]) {
        ShowToastWithText(@"请选择区域");
        return;
    }
    if ([self.addressTextView.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入详细地址");
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"userId"] = USER_ID;
    param[@"name"] = self.nameTextField.text;
    param[@"phone"] = self.phoneTextField.text;
    param[@"area"] = self.areaID;
    param[@"address"] = self.addressTextView.text;
    if (self.defaulButton.selected == YES) {
        param[@"isDefault"] = @"1";
    } else {
        param[@"isDefault"] = @"0";
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetWorkTool POST:saveUserAddress param:param success:^(id dic) {
        if (self.addSuccessfulClick) {
            self.addSuccessfulClick();
        }
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        NSString *msg = dic[@"msg"];
        ShowToastWithText(msg);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
    
}
// 过滤空格
- (NSString *)filterSpaces:(NSString *)text{
    text = [text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    text = [text stringByReplacingOccurrencesOfString:@" " withString:@""];
    text = [text stringByReplacingOccurrencesOfString:@"\n"withString:@""];
    return text;
}
/**
 *  过滤字符串中的emoji
 */
- (NSString *)disable_emoji:(NSString *)text{
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^\\u0020-\\u007E\\u00A0-\\u00BE\\u2E80-\\uA4CF\\uF900-\\uFAFF\\uFE30-\\uFE4F\\uFF00-\\uFFEF\\u0080-\\u009F\\u2000-\\u201f\r\n]"options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *modifiedString = [regex stringByReplacingMatchesInString:text
                                                               options:0
                                                                 range:NSMakeRange(0, [text length])
                                                          withTemplate:@""];
    return modifiedString;
}
@end
